﻿using GWB.EntityGenerator.Entity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GWB.EntityGenerator.Method
{
    public class BaseMethod
    {

        #region 类处理
        public static string ClassArrange(string namespaceName, string tableName, List<BaseTableEntity> list)
        {

            var namespaceStr = "";
            var tableStr = "";
            var fieldStr = "";
          


            foreach (var item in list)
            {
                fieldStr += $" \r\n {GetSpace(6)}/// <summary> \r\n {GetSpace(6)}/// {item.Explain} \r\n {GetSpace(6)}/// </summary> " +
                    $"\r\n {GetSpace(6)}public {StrSqlType(item.Type) } {item.Name} {{ get; set; }}";
            }
            tableStr = $"{GetSpace(2)}public  class {tableName}\r\n {GetSpace(3)}{{ {fieldStr} \r\n \r\n{GetSpace(3)}}}";
            if (!string.IsNullOrWhiteSpace(namespaceName))
            {
                namespaceStr = $"using System;\r\nusing System.Text;\r\nnamespace{GetSpace(1)}{namespaceName}\r\n {{\r\n{tableStr}\r\n }}";
                return namespaceStr;
            }
            return tableStr;

        }
        #endregion

        public static string StrSqlType(string strType)
        {
            switch (strType)
            {
                case "bigint":
                    return "long";
                case "int":
                    return "int";
                case "smallint":
                    return "short";
                case "tinyint":
                    return "short";
                case "bit":
                    return "boolean";
                case "decimal":
                    return "decimal";
                case "numeric":
                    return "decimal";
                case "money":
                    return "string";
                case "smallmoney":
                    return "string";
                case "float":
                    return "float";
                case "real":
                    return "float";
                case "datetime":
                    return "DateTime";
                case "smalldatetime":
                    return "DateTime";
                case "char":
                    return "char";
                case "varchar":
                    return "string";
                case "text":
                    return "string";
                case "nchar":
                    return "string";
                case "nvarchar":
                    return "string";
                case "ntext":
                    return "string";
                case "binary":
                    return "byte";
                case "varbinary":
                    return "byte";
                case "image":
                    return "string";
                case "uniqueidentifier":
                    return "Guid";
                default:
                    return "string";
            }
        }


        public static string GetSpace(int num)
        {
            string str = "";
            for (int i = 0; i < num; i++)
            {
                str += " ";
            }
            return str;
        }
        public static void SetFont(RichTextBox richTextBox, string namespaceName, string tableName, List<BaseTableEntity> list) 
        {
           
            foreach (var item in list)
            {
                ChangeColor(richTextBox, @" /// <summary>", "96,139,78", 9);
                ChangeColor(richTextBox, $"/// {item.Explain}", "96,139,78", 9);
                ChangeColor(richTextBox, "/// </summary>", "96,139,78", 9);
                ChangeColor(richTextBox,item.Name , "78,201,176", 0);
            }
            if (!string.IsNullOrWhiteSpace(namespaceName))
            {
                ChangeColor(richTextBox, $"{namespaceName}", "78,201,176", 0);
            }
            ChangeColor(richTextBox, $"{tableName}", "78,201,176", 0);

        }

        /// <summary>
        /// 设置字符串颜色
        /// </summary>
        /// <param name="richTextBox"></param>
        /// <param name="str"></param>
        public static void ChangeColor(RichTextBox richTextBox, string str, string color,int fontSize)
        {
            var listColor = color.Split(',').ToList();

            ArrayList list = GetIndexArray(richTextBox.Text, str);
            for (int i = 0; i < list.Count; i++)
            {
                int index = (int)list[i];
                richTextBox.Select(index, str.Length);
                if (fontSize>0)
                {
                    richTextBox.SelectionFont = new Font("宋体", fontSize);
                }
                if (color != null&& listColor.Count>0)
                {
                    richTextBox.SelectionColor = Color.FromArgb(Convert.ToInt32(listColor[0]), Convert.ToInt32(listColor[1]), Convert.ToInt32(listColor[2]));
                }
            

            }
        }

        public static ArrayList GetIndexArray(String inputStr, String findStr)
        {
            ArrayList list = new ArrayList();
            int start = 0;
            while (start < inputStr.Length)
            {
                int index = inputStr.IndexOf(findStr, start);
                if (index >= 0)
                {
                    list.Add(index);
                    start = index + findStr.Length;
                }
                else
                {
                    break;
                }
            }
            return list;

        }


    }
}
